package com.aopcinemas.cinema.behaviour;

import jade.content.lang.Codec.CodecException;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.core.Agent;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.RefuseException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.AchieveREResponder;

import java.util.List;

import com.aopcinemas.cinema.CinemaAgent;
import com.aopcinemas.cinema.dao.ClientDao;
import com.aopcinemas.cinema.dao.InvoiceDao;
import com.aopcinemas.cinema.dao.SessionDao;
import com.aopcinemas.cinema.model.Client;
import com.aopcinemas.cinema.model.Invoice;
import com.aopcinemas.cinema.model.Schedule;
import com.aopcinemas.cinema.model.Session;
import com.aopcinemas.cinema.ontology.OrderRequest;

public class OrderInfoBehavior extends AchieveREResponder {

    private static final long serialVersionUID = 1L;

    public OrderInfoBehavior(Agent a, MessageTemplate mt) {
        super(a, mt);
    }

    @Override
    protected ACLMessage handleRequest(ACLMessage request) throws NotUnderstoodException, RefuseException {
        try {
            CinemaAgent agent = (CinemaAgent) myAgent;

            Action action = (Action) myAgent.getContentManager().extractContent(request);
            OrderRequest orderRequest = (OrderRequest) action.getAction();

            InvoiceDao invoiceDao = agent.getInvoiceDao();
            SessionDao sessionDao = agent.getSessionDao();
            ClientDao clientDao = agent.getClientDao();

            ACLMessage reply = request.createReply();
            reply.setPerformative(ACLMessage.INFORM);
            // sessions
            List<Session> sessions = sessionDao.getSessions(orderRequest.getSchedule(), true);
            if (sessions.size() < orderRequest.getNumberOfSessions()) {
                reply.setPerformative(ACLMessage.REFUSE);
                reply.setContent("Not enought seats");
                return reply;
            }
            sessions = sessions.subList(0, orderRequest.getNumberOfSessions());
            // price
            double price = calculatePrice(sessions, orderRequest.getSchedule());
            // client
            Client client = clientDao.getClient(orderRequest.getPerson().getUid());
            if (client == null) {
                client = new Client(orderRequest.getPerson());
                clientDao.addClient(client);
            }
            // invoice
            Invoice invoice = new Invoice();
            invoice.setClient(client);
            invoice.setPrice(price);
            invoice.setSessions(sessions);
            invoiceDao.addInvoice(invoice);

            orderRequest.setInvoice(invoice);

            myAgent.getContentManager().fillContent(reply, action);
            return reply;
        } catch (CodecException e) {
            e.printStackTrace();
        } catch (OntologyException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("Something is wrong");
    }

    private double calculatePrice(List<Session> sessions, Schedule schedule) {
        double price = 0;
        for (int i = 0; i < sessions.size(); i++) {
            price += schedule.getBasePrice();
        }
        return price;
    }
}
